#include "StdAfx.h"

CSnake::CSnake(void)
{
	_vSections.clear();
	_bAddSection = false;
}

CSnake::~CSnake(void)
{
}

void CSnake::Move(RECT *clientRect)
{

					if(_bAddSection)
					{
						RECT r;
						r.left = this->_ptLocation.x;
						r.right = r.left + SEGMENT_WIDTH;
						r.top = this->_ptLocation.y;
						r.bottom = r.top + SEGMENT_HEIGHT;
						_vSections.push_back(r);		
						_bAddSection = false;
					}

						if(_vSections.size() > 0)
						{
							for(int i = (int)_vSections.size() - 1; i >= 0; i--)
							{
								if(i != 0)
								{
									_vSections[i].bottom = _vSections[i - 1].bottom;
									_vSections[i].left = _vSections[i - 1].left;
									_vSections[i].right = _vSections[i - 1].right;
									_vSections[i].top = _vSections[i - 1].top;
								}
								else
								{									
									_vSections[i].left = this->_ptLocation.x;
									_vSections[i].right = _vSections[i].left + SEGMENT_WIDTH;
									_vSections[i].top = this->_ptLocation.y;
									_vSections[i].bottom = _vSections[i].top + SEGMENT_HEIGHT;
								}

							}
						}

					


					switch(this->_nDirection)
					{
					case DIRECTION_LEFT:
						if((this->_ptLocation.x - SEGMENT_WIDTH) < 0)
						{
							SetLocation(clientRect->right - SEGMENT_WIDTH, this->_ptLocation.y);
						}
						else
						{
							SetLocation(this->_ptLocation.x - SEGMENT_WIDTH, this->_ptLocation.y);
						}
						break;

					case DIRECTION_RIGHT:
						if((this->_ptLocation.x + SEGMENT_WIDTH) >= clientRect->right)
						{
							SetLocation(clientRect->left, this->_ptLocation.y);
						}
						else
						{
							SetLocation(this->_ptLocation.x + SEGMENT_WIDTH, this->_ptLocation.y);
						}
						break;

					case DIRECTION_UP:
						if((this->_ptLocation.y - SEGMENT_HEIGHT) < 0)
						{
							SetLocation(this->_ptLocation.x, clientRect->bottom - SEGMENT_HEIGHT);
						}
						else
						{
							SetLocation(this->_ptLocation.x, this->_ptLocation.y - SEGMENT_HEIGHT);
						}
						break;

					case DIRECTION_DOWN:
						if((this->_ptLocation.y + SEGMENT_HEIGHT) >= clientRect->bottom)
						{
							SetLocation(this->_ptLocation.x, clientRect->top);
						}
						else
						{
							SetLocation(this->_ptLocation.x, this->_ptLocation.y + SEGMENT_HEIGHT);
						}
						break;
					}



}

void CSnake::SetDirection(WPARAM wParam)
{
					switch(wParam)
				{
				case VK_LEFT:
					// You can't go from right to left
					if(_nDirection == DIRECTION_RIGHT)
					{
						break;
					}
					else
					{
						_nDirection = DIRECTION_LEFT;
					}
					break;

				case VK_RIGHT:
					// You can't go from left to right
					if(_nDirection == DIRECTION_LEFT)
					{
						break;
					}
					else
					{
						_nDirection = DIRECTION_RIGHT;
					}
					break;

				case VK_UP:
					// You can't go from down to up
					if(_nDirection == DIRECTION_DOWN)
					{
						break;
					}
					else
					{	
						_nDirection = DIRECTION_UP;
					}
					break;

				case VK_DOWN:
					// You can't go from up to down
					if(_nDirection == DIRECTION_UP)
					{
						break;
					}
					else
					{
						_nDirection = DIRECTION_DOWN;
					}
					break;
				}
}


int CSnake::GetDirection()
{
	return _nDirection;
}


void CSnake::AddSection()
{
	_bAddSection = true;
}

std::vector<RECT>* CSnake::GetSections()
{
	return &_vSections;
}
